package com.ejoysoft.servlet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.ejoysoft.common.Globa;
import com.ejoysoft.wlanyz.business.Ap;

public class APInfoServlet extends HttpServlet implements Servlet
{
	
	private Document document2;
	private static final long serialVersionUID = 217251451801586160L;
	public APInfoServlet()
	{
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException, IOException {	// TODO Auto-generated method stub
		try {
			
		this.execute(req, resp);
		}catch(Exception e) {
			e.printStackTrace();
			throw new IOException(e.getMessage());
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
		System.err.println("11");
		this.doGet(req, resp);
	}

	private void execute(HttpServletRequest req, HttpServletResponse resp) throws IOException
	{
		 Globa globa = new Globa(); 
		 String content1 =  req.getParameter("ApInfo");
		 System.out.println("=======content1========"+content1);
		 if(!content1.equals("")){
			 InputStream is = new ByteArrayInputStream(content1.getBytes());//注意content的值是否为空，做判断。
			  if(readXML(is)){
				  resp.getWriter().write(returnResult("OK").toString());
			  }else {
				  resp.getWriter().write(returnResult("NO").toString());
			}
			 
		 }else{
			 resp.getWriter().write(returnResult("NO").toString());
		 }

	     globa.closeCon();
 
	}

	private StringBuffer returnResult(String result)
	{
		StringBuffer sbReturnContent = new StringBuffer("<?xml version='1.0' encoding='utf-8'?> ");
		sbReturnContent.append("<info>");
		sbReturnContent.append("<result>"+result+"</result>");
		System.out.println("++++result=="+result);
		sbReturnContent.append("</info>");
		return sbReturnContent;
	}
	public boolean readXML(InputStream is) { 
		 Globa globa = new Globa(); 
	    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
	    DocumentBuilder builder = null; 
		  try { 
			 
		      builder = factory .newDocumentBuilder();    
		      Document document = builder.parse(is); 
		      Element rootElement = document.getDocumentElement();  
		      NodeList list1 = rootElement.getElementsByTagName("strMac"); 
		      NodeList list2 = rootElement.getElementsByTagName("strLatitude"); 
		      NodeList list3 = rootElement.getElementsByTagName("strLongtitude"); 
		      Element element1 = (Element) list1.item(0); 
		      Element element2 = (Element) list2.item(0); 
		      Element element3 = (Element) list3.item(0); 
		      Ap ap = new Ap(globa,false);
		      ap.setApMac( element1.getChildNodes().item(0).getNodeValue());
		      String latlng =element3.getChildNodes().item(0).getNodeValue()+ "," + element2.getChildNodes().item(0).getNodeValue() ;
		     System.err.println("mac:"+element1.getChildNodes().item(0).getNodeValue());
		     System.err.println("latlng:"+latlng);
		      ap.setApLatlng(latlng);
		      
		      boolean isTrue=ap.update();
		      if(!isTrue){
		    	  System.out.println("result" + isTrue); 
		    	  globa.closeCon();
		    	  return false;//数据修改出错，返回失败
		      }else{
			      globa.closeCon();
			      System.out.println("result" + isTrue+"==========="); 
			      return  true;
		      }
		  } catch (Exception e) { 
		      System.out.println("exception:" + e.getMessage()); 
		      globa.closeCon();
		      return false;
		  } 
		 } 
	public Object evaluate(String expression, QName type) {
		XPath xpath = XPathFactory.newInstance().newXPath();
		Object node = null;
		try {
			node = xpath.evaluate(expression, document2, type);
		} catch (XPathExpressionException e) {
			System.err.println(e);
		}
		return node;
	}
}
